<?php

ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );
require_once('/home/cyberpower678/database.inc');

$site = Peachy::newWiki( "cyberbot i" );


$starting_timestamp = '20090801000000';
$contestant_points = array();
$points_page_name = 'Wikipedia:WikiCup/History/2010';
//$contestant_page_name = 'Wikipedia:WikiCup/History/2009/Contestants/Machine';

$contestant_page = initPage($points_page_name);

preg_match_all("/\{\{Wikipedia:WikiCup\/Participant\|(.*?)\}\}/i",$contestant_page->get_text(), $contestants, PREG_PATTERN_ORDER);
$contestants = $contestants[1];

//$contestants = array( 'IShadowed' );
print_r($contestants);

foreach( $contestants as $contestant ) {
        echo "Starting parsing for user $contestant...\n\n";
        
        $points = array();
        
        /*$mnsp_minor_count_query = "SELECT COUNT(*) as count FROM revision JOIN page ON page_id = rev_page WHERE rev_user_text = '$cont_safe' AND page_namespace = '0' AND rev_minor_edit = '1' AND rev_timestamp > '$starting_timestamp' AND rev_comment NOT LIKE '%[[WP:HG|HG]]%' AND rev_comment NOT LIKE '%[[WP:AWB|AWB]]%' AND rev_comment NOT LIKE '%[[WP:TW|TW]]%';";
        $mnsp_minor_count = mysql_query($mnsp_minor_count_query);
        if( !$mnsp_minor_count ) die( "Mysql Error:" . mysql_error() . "\n\n" );
        $mnsp_minor_count = mysql_fetch_assoc($mnsp_minor_count);
        $mnsp_minor_count = $mnsp_minor_count['count'];
        $mnsp_minor_points = $mnsp_minor_count * 0.01;
        
        $mnsp_major_count_query = "SELECT COUNT(*) as count FROM revision JOIN page ON page_id = rev_page WHERE rev_user_text = '$cont_safe' AND page_namespace = '0' AND rev_minor_edit = '0' AND rev_timestamp > '$starting_timestamp' AND rev_comment NOT LIKE '%[[WP:HG|HG]]%' AND rev_comment NOT LIKE '%[[WP:AWB|AWB]]%' AND rev_comment NOT LIKE '%[[WP:TW|TW]]%';";
        $mnsp_major_count = mysql_query($mnsp_major_count_query);
        if( !$mnsp_major_count ) die( "Mysql Error:" . mysql_error() . "\n\n" );
        $mnsp_major_count = mysql_fetch_assoc($mnsp_major_count);
        $mnsp_major_count = $mnsp_major_count['count'];
        $mnsp_major_points = $mnsp_major_count * 0.1;
        
        $points['mainspace'] = ceil( $mnsp_major_points + $mnsp_minor_points );*/
        
        $contestant_sub_page_name = 'Wikipedia:WikiCup/History/2010/Submissions/'.$contestant;

        $contestant_submissions = initPage($contestant_sub_page_name)->get_text();

        $m = preg_split('/===(.*?)===/',$contestant_submissions);
        
        $sections = array(
                'FA' => $m[1],//
                'GA' => $m[2],//
                'FL' => $m[3],//
                'FP' => $m[4],//
                'FS' => $m[5],//
                'FPO' => $m[6],//
                'FT' => $m[7],//
                'GT' => $m[8],//
                'DYK' => $m[9],//
                'ITN' => $m[10],//
                'VP' => $m[11],
        );
        
        
        //START DYK
        $dyk_lines = explode("\n",$sections['DYK']);
        $dyk_count = 0;
        foreach($dyk_lines as $dyk) {
                if( preg_match('/^\#./',$dyk) ) $dyk_count++;
        }
        $points['DYK'] = ceil( $dyk_count * 10 );
        
        //START ITN
        $itn_lines = explode("\n",$sections['ITN']);
        $itn_count = 0;
        foreach($itn_lines as $itn) {
                if( preg_match('/^\#./',$itn) ) $itn_count++;
        }
        $points['ITN'] = ceil( $itn_count * 10 );
        
        //START FPO
        $fpo_lines = explode("\n",$sections['FPO']);
        $fpo_count = 0;
        foreach($fpo_lines as $fpo) {
                if( preg_match('/^\#./',$fpo) ) $fpo_count++;
        }
        $points['FPO'] = ceil( $fpo_count * 35 );
        
        //START GA
        $ga_lines = explode("\n",$sections['GA']);
        $ga_count = 0;
        foreach($ga_lines as $ga) {
                if( preg_match('/^\#./',$ga) ) $ga_count++;
        }
        $points['GA'] = ceil( $ga_count * 40 );
        
        //START FL
        $fl_lines = explode("\n",$sections['FL']);
        $fl_count = 0;
        foreach($fl_lines as $fl) {
                if( preg_match('/^\#./',$fl) ) $fl_count++;
        }
        $points['FL'] = ceil( $fl_count * 40 );
        
        //START FP
        $fp_lines = explode("\n",$sections['FP']);
        $fp_count = 0;
        foreach($fp_lines as $fp) {
                if( preg_match('/^\#./',$fp) ) $fp_count++;
        }
        $points['FP'] = ceil( $fp_count * 35 );
        
        //START FS
        $fs_lines = explode("\n",$sections['FS']);
        $fs_count = 0;
        foreach($fs_lines as $fs) {
                if( preg_match('/^\#./',$fs) ) $fs_count++;
        }
        $points['FS'] = ceil( $fs_count * 35 );
        
        //START FA
        $fa_lines = explode("\n",$sections['FA']);
        $fa_count = 0;
        foreach($fa_lines as $fa) {
                if( preg_match('/^\#./',$fa) ) $fa_count++;
        }
        $points['FA'] = ceil( $fa_count * 100 );
        
        //START FT
        $ft_lines = explode("\n",$sections['FT']);
        $ft_count = 0;
        foreach($ft_lines as $ft) {
                if( preg_match('/^\#\#./',$ft) ) $ft_count++;
        }
        $points['FT'] = ceil( $ft_count * 15 );
        
        //START GT
        $gt_lines = explode("\n",$sections['GT']);
        $gt_count = 0;
        foreach($gt_lines as $gt) {
                if( preg_match('/^\#\#./',$gt) ) $gt_count++;
        }
        $points['GT'] = ceil( $gt_count * 10 );
        
        //START VP
        $vp_lines = explode("\n",$sections['VP']);
        $vp_count = 0;
        foreach($vp_lines as $vp) {
                if( preg_match('/^\#\#./',$vp) ) $vp_count++;
        }
        $points['VP'] = ceil( $vp_count * 5 );
        
        $total_points = array_sum( $points );

        $contestant_points[] = array(
                'name' => $contestant,
                'total' => $total_points,
                'points' => $points,
        );
}

print_r($contestant_points);

$points_page = initPage($points_page_name);
$points_page_text = $points_page_text_original = $points_page->get_text();

foreach( $contestant_points as $user ) {
        
        print_r($user);
        
        $name = $user['name'];
        //{{Wikipedia:WikiCup/Participant|03md}}||0||0||0||0||0||0||0||0||0||0||0||'''0'''
        preg_match( "/\{\{Wikipedia:WikiCup\/Participant\|".$name."}}\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|\d+\|\|'''\d+'''/i", $points_page_text, $m );
        
        $points = "{{Wikipedia:WikiCup/Participant|$name}}||";
        print_r($m);
        $names = array(
                'FA',// => $m[1],//
                'GA',// => $m[2],//
                'FL',// => $m[3],//
                'FP',// => $m[4],//
                'FS',// => $m[5],//
                'FPO',// => $m[6],//
                'FT',// => $m[7],//
                'GT',// => $m[8],//
                'DYK',// => $m[9],//
                'ITN',// => $m[10],//
                'VP'// => $m[11],
        );
        
        foreach( $names as $name ) {
                $points .= $user['points'][$name] . '||';
        }
        
        $points .= "'''{$user['total']}'''";
        //echo $points;
        $points_page_text = str_replace($m[0],$points,$points_page_text);
        //$points_page_text = preg_replace( '/\{\{Wikipedia:WikiCup\/Participant\|'.$name.'}}(.*?)$/i', $points, $points_page_text );
}

echo getTextDiff('unified', $points_page_text_original, $points_page_text);

$points_page->edit($points_page_text,"Bot: Updating WikiCup table",true);
